package com.byedu.course.util;

import com.byedu.domain.focus.FocusUserDO;
import com.byedu.domain.focus.vo.enumvos.FocusdepaTypeEnum;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.focus.FocusProjectService;
import com.byedu.focus.FocusUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Luo on 2017-08-25.
 */
public class ControllerUtil {

    public  static FocusUserVO checkuser(HttpServletRequest request,HttpServletResponse response) throws IOException {
        FocusUserVO focusUserDO = (FocusUserVO)request.getSession().getAttribute("user");
        if(focusUserDO == null){
            response.sendRedirect(request.getContextPath() + "/admin/login");
            return null;
        }else{
//            System.out.println("调整成功" + focusUserDO.toString());
            return focusUserDO ;
        }
    }

    public  static int saveuser(HttpServletRequest request,FocusProjectService focusProjectService,FocusUserService focusUserService,FocusUserVO focusUserVO) {
        List sizeList = focusProjectService.getFocusUnfinishedProjectVOListByTimeandprid(focusUserVO.getId(), null, null, null, null, null, focusUserVO.getPermission().getCode());
       if(sizeList != null){
           focusUserVO.setUf(sizeList.size());
       }

        request.getSession().setAttribute("user",focusUserVO);
     return  focusUserService.updateFocusUserUserId( new FocusUserDO(focusUserVO));
    }

    public  static String toHtml( FocusUserVO focusUserVO,String topage ,HttpServletRequest request,HttpServletResponse response) throws IOException {

        int permission = focusUserVO.getPermission().getCode();
        //老板权限对应的页面
        if(FocusdepaTypeEnum.BOSS.getCode() == permission){
            return topage+"";
        }
        //AE权限对应的页面
        if(FocusdepaTypeEnum.AE.getCode()== permission){
            return topage+"";
        }
        //AE权限对应的页面
        if(FocusdepaTypeEnum.ACCOUNT.getCode()== permission){
            return topage+"";
        }
        if(FocusdepaTypeEnum.CASHIER.getCode()== permission ){
            return topage+"";
        }

        return topage+"";
    }

    public static String getRemoteHost(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }
}
